package com.oa.ylms.apps.dao.organization.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.dao.organization.IPostInfoDao;
import com.oa.ylms.apps.entity.organization.PostInfoValue;

public class PostInfoDaoImpl extends HibernateDaoSupport implements
		IPostInfoDao {
	private List<PostInfoValue> list;
	private int numbers;

	@Override
	public int addPostInfo(PostInfoValue postInfo) throws Exception {
		if(postInfo != null){
			this.getHibernateTemplate().save(postInfo);
			return postInfo.getPostId();
		}
		return 0;
	}

	@Override
	public boolean editPostInfo(PostInfoValue postInfo) throws Exception {
		if(postInfo != null){
			try{
				this.getHibernateTemplate().update(postInfo);
			}catch(Exception e){
				return false;
			}
		}
		return true;
	}

	@Override
	public PostInfoValue queryByPostId(PostInfoValue postInfo)
			throws Exception {
		if(postInfo != null){
			return (PostInfoValue) this.getHibernateTemplate().get(PostInfoValue.class, postInfo.getPostId());
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PostInfoValue> queryPostInfo(
			final PostInfoValue postInfo) throws Exception {
			String hqlStr = "FROM PostInfoValue piv WHERE 1=1";
      if(postInfo.getSectionInfo() != null){
        hqlStr += " AND piv.sectionInfo = ?";
      }
      if(postInfo.getStates() != null){
        hqlStr += " AND piv.states = ?";
      }
      hqlStr += " AND piv.states <> ? ORDER BY piv.postCategory";
      final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Query query = session.createQuery(hql);
          int index = -1;
          if(postInfo.getSectionInfo() != null){
            query.setEntity(++index, postInfo.getSectionInfo());
          }
          if(postInfo.getStates() != null){
            query.setInteger(++index, postInfo.getStates());
          }
					query.setInteger(++index, IDEDStates.DELETE);
					list = query.list();
					return list;
				}
			});
			return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PostInfoValue> queryPostInfoWithPage(
		final PostInfoValue postInfo, final int rows, final int pageNo) throws Exception {
		String hqlStr = "FROM PostInfoValue piv WHERE 1=1";
		if(postInfo.getPostCategory() != null){
			hqlStr += " AND piv.postCategory = ?";
		}
		if(postInfo.getSectionInfo() != null){
			hqlStr += " AND piv.sectionInfo = ?";
		}
		if(postInfo.getStates() != null){
			hqlStr += " AND piv.states = ?";
		}
		hqlStr += " AND piv.states <> ?";
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if(postInfo.getPostCategory() != null){
					query.setEntity(++index, postInfo.getPostCategory());
				}
				if(postInfo.getSectionInfo() != null){
					query.setEntity(++index, postInfo.getSectionInfo());
				}
				if(postInfo.getStates() != null){
					query.setInteger(++index, postInfo.getStates());
				}
				query.setInteger(++index, IDEDStates.DELETE);
				query.setFirstResult(rows * (pageNo - 1));
				query.setMaxResults(rows);
				list = query.list();
				return list;
			}
		});
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int countTPages(final PostInfoValue postInfo, int rows) throws Exception {
		int pageNumbers = 0;
		String hqlStr = "FROM PostInfoValue piv WHERE 1=1";
		if(postInfo.getPostCategory() != null){
			hqlStr += " AND piv.postCategory = ?";
		}
		if(postInfo.getSectionInfo() != null){
			hqlStr += " AND piv.sectionInfo = ?";
		}
		if(postInfo.getStates() != null){
			hqlStr += " AND piv.states = ?";
		}
		hqlStr += " AND piv.states <> ?";
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
			throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if(postInfo.getPostCategory() != null){
					query.setEntity(++index, postInfo.getPostCategory());
				}
				if(postInfo.getSectionInfo() != null){
					query.setEntity(++index, postInfo.getSectionInfo());
				}
				if(postInfo.getStates() != null){
					query.setInteger(++index, postInfo.getStates());
				}
				query.setInteger(++index, IDEDStates.DELETE);
				list = query.list();
				return list;
			}
		});
		numbers = list.size();
		if (numbers <= rows) {
			pageNumbers = 1;
		} else {
			if (numbers % rows == 0) {
				pageNumbers = numbers / rows;
			} else {
				pageNumbers = numbers / rows + 1;
			}
		}
		return pageNumbers;
	}
	
	@Override
	public int getTotalPages() throws Exception {
		return getNumbers();
	}
	
	
	
	public List<PostInfoValue> getList() {
		return list;
	}

	public void setList(List<PostInfoValue> list) {
		this.list = list;
	}

	public void setNumbers(int numbers) {
		this.numbers = numbers;
	}

	public int getNumbers() {
		return numbers;
	}

}
